﻿using EONFULLSIN2.Core;
using EONFULLSIN2.Services.DB;
using EONFULLSIN2.Services.Device.DIOModelA;
using EONFULLSIN2.Services.Device.DIOModelB;
using EONFULLSIN2.Services.Device.JuYing;
using EONFULLSIN2.Services.Device.ZhongChuangZhiHe;
using EONFULLSIN2.Services.Device.ZhuoLan;
using EONFULLSIN2.Services.Device.ZMoiton;
using EONFULLSIN2.Services.Message;
using EONFULLSIN2.ViewModelBase;
using Prism.Events;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EONFULLSIN2.ViewModel
{
    public class MainViewModel : NavigationViewModel
    {
        private readonly IJuYingModbusService _juYingModbusService;
        private readonly IZhuoLanModbusService _zhuoLanModbusService;
        private readonly IZMoitonService _zMoitonService;
        private readonly IZczhModbusService _zczhModbusService;

        public MainViewModel(
            IRegionManager regionManager,
            IDialogService dialog,
            IMessageService messageBox,
            IEventAggregator eventAggregator,
            IDbOperate<IFreeSqlMainContext> mainDbOperate,
            IJuYingModbusService juYingModbusService,
            IZhuoLanModbusService zhuoLanModbusService,
            IZMoitonService zMoitonService,
            IZczhModbusService zczhModbusService
            )
            : base(regionManager, dialog, messageBox, eventAggregator, mainDbOperate) {
            this._juYingModbusService = juYingModbusService;
            this._zhuoLanModbusService = zhuoLanModbusService;
            this._zMoitonService = zMoitonService;
            this._zczhModbusService = zczhModbusService;
            ConnectInit();
        }


        #region Props

        public string GrowToken => "MainViewGrowToken";

        #endregion

        private void ConnectInit()
        {
            //JUYING连接
            _connectLogUIEvent.Publish($"{_juYingModbusService.IpAddress}:{_juYingModbusService.Port},正在连接(JUYING),...");
            _ = _juYingModbusService.ConnectAsync().ContinueWith(task =>
            {
                string message = "";
                if (task.Exception != null)
                {
                    message = task.Exception.Message;
                    _message.GrowError(message, GrowToken);
                }
                else
                {
                    message = $"{_juYingModbusService.IpAddress}:{_juYingModbusService.Port},连接情况(JUYING),Rlt={task.Result}";
                }
                _connectLogUIEvent.Publish(message);
            });

            //ZHUOLAN连接
            //_connectLogUIEvent.Publish($"{_zhuoLanModbusService.IpAddress}:{_zhuoLanModbusService.Port},正在连接(ZHUOLAN)...");
            //_ = _zhuoLanModbusService.ConnectAsync().ContinueWith(task =>
            //{
            //    string message = "";
            //    if (task.Exception != null)
            //    {
            //        message = task.Exception.Message;
            //        _messageBox.GrowError(message, GrowToken);
            //    }
            //    else
            //    {
            //        message = $"{_zhuoLanModbusService.IpAddress}:{_zhuoLanModbusService.Port},连接情况(JUYING),Rlt={task.Result}";
            //    }
            //    _connectLogUIEvent.Publish(message);
            //});

            _connectLogUIEvent.Publish($"{_zczhModbusService.IpAddress}:{_zczhModbusService.Port},正在连接(ZCZH)...");
            _ = _zczhModbusService.ConnectAsync().ContinueWith(task => {
                string message = "";
                if (task.Exception != null) {
                    message = task.Exception.Message;
                    _message.GrowError(message, GrowToken);
                } else {
                    message = $"{_zhuoLanModbusService.IpAddress}:{_zhuoLanModbusService.Port},连接情况(ZCZH),Rlt={task.Result}";
                }
                _connectLogUIEvent.Publish(message);
            });


            #region Adam
            ////adamCom1端口打开
            //_connectLogUIEvent.Publish($"{_adamComDIOModelAService.COMName},正在打开...");
            //_ = _adamComDIOModelAService.OpenComAsync().ContinueWith((task) =>
            //{
            //    string message = "";
            //    if (task.Exception != null)
            //    {
            //        message = task.Exception.Message;
            //    }
            //    else {
            //        message = $"{_adamComDIOModelAService.COMName},打开情况,Rlt={task.Result}";
            //    }
            //    _connectLogUIEvent.Publish(message);
            //});
            //if (_adamComDIOModelAService.IsOpen)
            //{
            //    _connectLogUIEvent.Publish($"{_adamComDIOModelAService.COMName},正在开启实时数据查询,...");
            //    //开启读数据
            //    _ = _adamComDIOModelAService.StartReadDOChannelStatusAsync().ContinueWith((task) =>
            //    {
            //        string message = "";
            //        if (task.Exception != null)
            //        {
            //            message = task.Exception.Message;
            //        }
            //        else
            //        {
            //            message = $"{_adamComDIOModelAService.COMName},实时数据查询已开启";
            //        }
            //        _connectLogUIEvent.Publish(message);
            //    });
            //}

            ////adamCom2端口打开
            //_connectLogUIEvent.Publish($"{_adamComDIOModelBService.COMName},正在打开...");
            //_ = _adamComDIOModelBService.OpenComAsync().ContinueWith((task) =>
            //{
            //    string message = "";
            //    if (task.Exception != null)
            //    {
            //        message = task.Exception.Message;
            //    }
            //    else
            //    {
            //        message = $"{_adamComDIOModelBService.COMName},打开情况,Rlt={task.Result}";
            //    }
            //    _connectLogUIEvent.Publish(message);
            //});
            //if (_adamComDIOModelBService.IsOpen)
            //{
            //    _connectLogUIEvent.Publish($"{_adamComDIOModelBService.COMName},正在开启实时数据查询,...");
            //    //开启读数据
            //    _ = _adamComDIOModelBService.StartReadDOChannelStatusAsync().ContinueWith((task) =>
            //    {
            //        string message = "";
            //        if (task.Exception != null)
            //        {
            //            message = task.Exception.Message;
            //        }
            //        else
            //        {
            //            message = $"{_adamComDIOModelBService.COMName},实时数据查询已开启";
            //        }
            //        _connectLogUIEvent.Publish(message);
            //    });
            //}
            #endregion

            _connectLogUIEvent.Publish($"{_zMoitonService.IpAddress},正在连接(ZMoiton)...");
            _ = _zMoitonService.ConnectAsync().ContinueWith(task => {
                string message = "";
                if (task.Exception != null) {
                    message = task.Exception.Message;
                    _message.GrowError(message, GrowToken);
                } else {
                    message = $"{_zMoitonService.IpAddress}:{_zMoitonService.Port},连接情况(ZMoiton),Rlt={task.Result}";
                }
                _connectLogUIEvent.Publish(message);
            });

        }

        protected override void OnViewLoaded()
        {
            //base.OnViewLoaded();
        }



    }
}
